package saas.baseinfo.service.api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import saas.baseinfo.service.global.BusinessException;
import saas.baseinfo.service.utils.MessageUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author guan.xin
 * @title ApiController
 * @description
 * @create 2022-05-24 15:15
 * @since JDK8
 */
@Slf4j
@RestController
@RequestMapping(value = "/api")
public class ApiController {

    @GetMapping(value = "/lang")
    public String getLang() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        requestAttributes.setAttribute("saasTenantCode", "NIKE", RequestAttributes.SCOPE_REQUEST);
        RequestContextHolder.setRequestAttributes(requestAttributes, true);

        HttpServletRequest mainRequest = ((ServletRequestAttributes) requestAttributes).getRequest();

        log.error("主线程的租户：{}", mainRequest.getHeader("tenantCode"));

        new Thread(() -> {
            RequestAttributes requestAttributes1 = RequestContextHolder.currentRequestAttributes();
            HttpServletRequest subRequest = ((ServletRequestAttributes) requestAttributes1).getRequest();
            log.warn("子线程的租户初次获取：{}", subRequest.getHeader("tenantCode"));
            log.warn("子线程的租户初次获取：{}", requestAttributes1.getAttribute("saasTenantCode", RequestAttributes.SCOPE_REQUEST));
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.warn("子线程的租户再次获取：{}", subRequest.getHeader("tenantCode"));
            //log.warn("子线程的租户再次获取：{}", requestAttributes1.getAttribute("saasTenantCode", RequestAttributes.SCOPE_SESSION));
            log.warn(RequestContextHolder.currentRequestAttributes().toString());
            log.warn(RequestContextHolder.currentRequestAttributes().getAttributeNames(RequestAttributes.SCOPE_REQUEST).toString());
        }).start();

        log.info("主线程已结束");

        return LocaleContextHolder.getLocale().getLanguage();
    }

    @GetMapping(value = "/message")
    public String getMessage() {
        return MessageUtils.get("user.name");
    }

    @GetMapping(value = "/placeholder")
    public String getPlaceholder(@RequestParam String shopName) {
        return MessageUtils.get("shop.name", shopName);
    }

    @GetMapping(value = "/server_exception")
    public void serverException() {
        throw new RuntimeException();
    }

    @GetMapping(value = "/exception")
    public void exception() {
        throw new BusinessException(100001);
    }

    @GetMapping(value = "/log_print")
    public void getLogPrint() {
        log.info(MessageUtils.get("log.print"));
    }

    @GetMapping(value = "/log_placeholder")
    public void getPlaceholder() {
        log.info(MessageUtils.get("log.placeholder"), "tm_nike", MessageUtils.get("tm_nike"));
    }
}
